import datetime
import json
from db.datab import db
from .base import BaseHandler
from util.mongodb import my_collection

class Evaluate(BaseHandler):
    def post(self):
        data = json.loads(self.request.body)
        userid = data["userid"]
        content = data["content"]
        dish_id = data["dish_id"]
        create_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if not all([id,userid,content,dish_id,create_time]):
            return self.finish({"code":200,"msg":"字段不能为空"})
        document = {"userid": userid,"content": content,"dish_id": dish_id,"create_time":create_time}
        ret = my_collection.insert_one(document)
        print("ret>>",ret.inserted_id)
        return self.finish({"code": 200, "msg":"提交成功"})
    def get(self):
        dish_id = self.get_query_argument("code")
        query = {"dish_id": dish_id}
        document_list = my_collection.find(query)
        lst = []
        for document in document_list:
            lst.append({
                "userid":document["userid"],
                "content": document["content"],
                "dish_id": document["dish_id"],
                "create_time": document["create_time"],
            })
        return self.finish({"code": 200, "msg": "获取成功","data":lst})

class clandmarks(BaseHandler):
    def get(self):
        page = self.get_query_argument("page")
        page_size = self.get_query_argument("page_size")
        start = (int(page)-1) * int(page_size)
        sql = "select count(*) as a from (select * from landmarks union all select * from landmarks1 union all select * from landmarks2) as t"
        ress = db.findOne(sql)
        total = ress['a']
        sqls = "select *  from (select * from landmarks union all select * from landmarks1 union all select * from landmarks2) as t limit %d,%d"%(int(start),int(page_size))
        res = db.findAll(sqls)
        for i in res:
            i["interest_rate"] = float(i["interest_rate"])
            i["code"] = str(i["code"])
            i["audit_time"] = datetime.datetime.strftime(i["audit_time"],"%Y-%m-%d %H:%M:%S")
            if i["due_time"]:
                i["due_time"] = datetime.datetime.strftime(i["due_time"],"%Y-%m-%d %H:%M:%S")

        return self.finish({"code": 200,"msg":"获取成功","data":res,"total":total})
